<?php

	class Metric_proxy extends Model
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		
		public function getCurrentYear()
		{
			$currentyear = $this->db->query('
													SELECT		CAST(ce.current_year AS CHAR(4)) AS current_year
													FROM		cwr_event ce
											    ');
												
			return $currentyear->first_row()->current_year;
		}

		public function getPersonGoal( $personId )
		{
			$curyear = $this->getCurrentYear();
			
			$getMetric = $this->db->query('
											SELECT		cwr.funds_goal as metric
											FROM		cwr_reg_history cwr
											WHERE		cwr.person_id_fk = ' . $this->db->escape( $personId ) . '
											AND			cwr.year = ' . $this->db->escape( $curyear ) . '
										  ');
										  
			return $getMetric->row()->metric;
		}
		
		
		public function getPersonTotalRaised( $personId )
		{
			$curyear = $this->getCurrentYear();
			
			$getMetric = $this->db->query('
											SELECT				ROUND(IFNULL(crh.amount_committed,0) + IFNULL((SELECT SUM(cd.amount_paid) FROM cwr_donor cd WHERE cd.donor_id_fk=p.person_id AND cd.year = ' . $this->db->escape( $curyear ) . '),0) + IFNULL((SELECT SUM(cs.amount_paid) FROM cwr_sponsor cs WHERE cs.sponsoree_id_fk=p.person_id AND cs.year = ' . $this->db->escape( $curyear ) . '),0)) AS metric
											FROM 				person p
											LEFT OUTER JOIN 	cwr_reg_history crh ON crh.person_id_fk = p.person_id
											WHERE				p.enabled = 1
											AND					p.person_id = ' . $this->db->escape( $personId ) . '
											AND					crh.year = ' . $this->db->escape( $curyear ) . '
										  ');
										  
			return $getMetric->row()->metric;
		}
		
		
		public function getGoal()
		{
			$getMetric = $this->db->query('
											SELECT		ce.total_goal as metric
											FROM 		cwr_event ce
											WHERE		ce.enabled = 1
											AND			ce.current = 1;
										  ');
										  
			return $getMetric->row()->metric;
		}
		
		
		public function getTotalRaised()
		{
			$curyear = $this->getCurrentYear();
			
			$getMetric = $this->db->query('
											SELECT				ROUND(SUM(IFNULL(crh.amount_committed,0)+IFNULL((SELECT SUM(cd.amount_paid) FROM cwr_donor cd WHERE cd.donor_id_fk=p.person_id AND cd.year='.$this->db->escape( $curyear ).'),0) + IFNULL((SELECT SUM(cs.amount_paid) FROM cwr_sponsor cs WHERE cs.sponsoree_id_fk=p.person_id AND cs.year='.$this->db->escape( $curyear ).'),0))) AS metric
											FROM 				person p
											LEFT OUTER JOIN 	cwr_reg_history crh 
											ON 					crh.person_id_fk = p.person_id AND crh.year = ' . $this->db->escape( $curyear ) . '
											WHERE				p.enabled = 1
										  ');
										  
			return $getMetric->row()->metric;
		}
		
		
		public function getNumberRegistered()
		{
			$curyear = $this->getCurrentYear();
			
			$getMetric = $this->db->query('
											SELECT		COUNT(cwr_reg_history_id) AS metric
											FROM 		cwr_reg_history crh
											WHERE		crh.enabled = 1
											AND			crh.year = ' . $this->db->escape( $curyear ) . '
										  ');
										  
			return $getMetric->row()->metric;
		}
		
		public function getNumberParticipating()
		{
			$curyear = $this->getCurrentYear();
			
			$getMetric = $this->db->query('
											SELECT		SUM(crh.number_walkers+crh.number_runners) AS metric
											FROM 		cwr_reg_history crh
											WHERE		crh.enabled = 1
											AND			crh.year = ' . $this->db->escape( $curyear ) . '
										  ');
										  
			return $getMetric->row()->metric;
		}
	}
?>